
if(!window.console){
	// Just don't crash
	window.console = {
		log : function(){
		}
	}
}

// Namespace for native OSGi API.
// Stubs / test version for reqular browser. Implemented by OsgiRelay.java on Android app.
if(!window._OsgiRelay){
	window._OsgiRelay = {
		postOSGiEvent : function(topic, properties)
		{	
			console.log(topic, properties)
		},
		
		postAndroidIntent : function(properties)
		{	
			console.log(properties)
		},
		
		setupOSGi2IntentConversion : function(topic, intent)
		{
			console.log(topic, intent)
		},
		setupIntentListener : function(intentAction){
			console.log(intentAction);
		}
	};
}

var C_OSAMI = function(){
	this.osgiEventListeners = {};
	
	// topic to android intent name
	this.osgi_conversions = {};
	
	this.setupOSGi2IntentConversion = function(topic, intent)
	{
		if(this.osgi_conversions[topic]){
			log.console("WARNING: topic already mapped to intent:" + topic + "=>" + intent);
		}
		this.osgi_conversions[topic] = intent;
		_OsgiRelay.setupOSGi2IntentConversion(topic, intent);
	};
	
	/** @param: topic - Name of OSGi topic to listen
	 *  @param: listener - A function accepting 2 parameters: topic and event object(from json)
	 *  			       Event object holds the OSGi event properties.
	 */
	this.addOsgiEventListener = function(topic, listener)
	{
		var listeners = this.osgiEventListeners[topic];
		if(listeners === undefined){
			this.osgiEventListeners[topic] = [listener];
		}
		else{
			this.osgiEventListeners[topic].push(listener);
		}
	};
	
	/** 
	 * Called from native when receiving OSGi event via Intent. 
	 * Calls each event handler added via addOsgiEventListener.
	 * 
	 * */
	this._processOSGiEvent = function(topic, event_json)
	{
		console.log("processing osgi event")
		var listeners;
		listeners = this.osgiEventListeners[topic];
		if(!listeners){
			console.log("ERROR: no listener for " + topic );
			return;
		}
		var i;
		for(i=0;i<listeners.length;i++)
		{
			listeners[i](topic, event_json);
		}
	};
	
	/** Creates an internal(Java) mapping to send android intent
	 * data to javascript. To add listener functions, see addIntentListener()
	 * @param intentname: ID of the Intent to listen. 
	 */
	this.setupIntentListener = function(intentname){
		_OsgiRelay.setupIntentListener(intentname);
	};
	
	/**
	 * Called from native when receiving Android intent
	 * registered via setupIntentListener(intentname)
	 * 
	 */
	this._processAndroidIntent = function(data){
		console.log("received android intent " + data.intent);
		try
		{		
			var listeners;
			listeners = this.intentListeners[data.intent];
			if(!listeners){
				console.log("ERROR: no listener for " + data.intent);
				return;
			}
			
			var i;
			for(i=0;i<listeners.length;i++)
			{
				listeners[i](data);
			}
		}
		catch(e)
		{
			console.log(String(e));
		}
	};
	
	this.intentListeners = {};
	this.addIntentListener = function(intentname, listener)
	{
		var listeners = this.intentListeners[intentname];
		if(!listeners){
			this.intentListeners[intentname] = [listener];
		}
		else{
			this.intentListeners[intentname].push(listener);
		}
	};
	
	this.postOSGiEvent = function(topic, properties){
		properties = JSON.stringify(properties);
		_OsgiRelay.postOSGiEvent(topic, properties);
	};
	
	this.postAndroidIntent = function(properties){
		properties = JSON.stringify(properties);
		_OsgiRelay.postAndroidIntent(properties);
	}
	
};

var OSAMI = new C_OSAMI();

OSAMI.HID_QUERY = "org.osami.HID_MANAGER_QUERY";
OSAMI.HID_RESPONSE = "org.osami.HID_MANAGER_RESPONSE";

console.log("OSAMI.HID_QUERY:" + OSAMI.HID_QUERY)